package BluetoothFileMgr.start;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class ServerMgr extends Thread{

	DataOutputStream out=null;
	DataInputStream in=null;
	TextView tv;
	private Handler handler = new Handler();
	
	public ServerMgr(DataInputStream in,DataOutputStream out,TextView tv)
	{
		this.out=out;
		this.in=in;
		this.tv=tv;
	}
	
	public void print(final String s)
	{
		handler.post(new Runnable() {
            public void run() {
                tv.append("\n"+s);
            }
        });
	}
	
	public void run()
	{
		String directory=Environment.getExternalStorageDirectory().toString();
		String rcvd="";
		print(directory);
		while(true)
		{
			File f=new File(directory);
			String str=directory;
			if(f.list().length==0)
			{
				str+="!!empty!";
			}
			else
			{
				for(File file:f.listFiles())
				{
					if(file.isDirectory())
						str+="!!dir!"+file.getName()+"!"+file.length();
					else
						str+="!!file!"+file.getName()+"!"+file.length();
				}
			}
			try
			{
				out.writeUTF(str);
				rcvd=in.readUTF();
			}
			catch(Exception e)
			{
				
			}
			print(rcvd);
    		String[] sArr=rcvd.split("!!");
    		if(sArr[0].equals("GOTO"))
    		{
    			directory=directory+"/"+sArr[1];
    		}
    		else if(sArr[0].equals("BACK"))
    		{
    			if(!directory.equals("/mnt"))
    			{
    				String[] sA=directory.split("/");
    				directory="";
    				int j=1;
    				while(j<sA.length-1)
    				{
    					directory=directory+"/"+sA[j];
    					j++;
    				}
    			}
    		}
    		else if(sArr[0].equals("ADD"))
    		{
    			String lastDir=directory;
    			directory+="/"+sArr[1];
    			File fNew=new File(directory); 
    			fNew.mkdir();
    			directory=lastDir;
    		}
    		else if(sArr[0].equals("DOWNLOAD"))
    		{
    			FileInputStream f2send=null;
				
    			
    			////////////////////////////////////////////
    			//RunFile abc=new RunFile(directory+"/"+sArr[1]);
    			//while(true)
    			//{
    			////////////////////////////////////////////
    			 
    			try 
    			{
	    			print("Sending...");
					//msg.obj="Sending...";
					//handler.sendMessage(msg);
	    			//if (socket != null)
					{
	    				byte[] b=null;
						try 
						{
							File f1=new File(directory+"/"+sArr[1]);
							f2send = new FileInputStream(f1);	
							b=new byte[(int) f1.length()];
							f2send.read(b);
							//send = fileSocket.getOutputStream();
							//send=socket.getOutputStream();
		    				out.write(b);
		    				f2send.close();
		    				//send.close();
							
						} 
						catch (IOException e) 
						{
							
						}
	    				
	    				//send.close();
					}
	    			print("Sent");
					//fileSocket.close();
					//msg.obj="Sent";
					//handler.sendMessage(msg);
				}
    			catch (Exception e) 
    			{
    				print(e.getMessage());
					//msg.obj=e.getMessage();
					//handler.sendMessage(msg);
				}
				
				//////////////////////////////////////////////
    			//msg.obj="Sending "+directory+"/"+sArr[1]+"...";
	    		//handler.sendMessage(msg);
    			
    			//DataOutputStream send=new DataOutputStream(fileSocket.getOutputStream());
    			
    			//DataInputStream f2send=new DataInputStream(new FileInputStream(directory+"/"+sArr[1]));
    			//String s=f2send.readUTF();
    			//send.writeUTF(s);
    			//
    			//////////////////////////////////////////////
    			
    			
    		}
    		else if (sArr[0].equals("DELETE"))
			{
				for(File f1 : f.listFiles())
				{
					if(f1.getName().equals(sArr[1]))
					{
						f1.delete();
					}
				}
			}
    		else if (sArr[0].equals("RENAME"))
			{
				for(File f1 : f.listFiles())
				{
					if(f1.getName().equals(sArr[1]))
					{
						f1.renameTo(new File(directory+"/"+sArr[2]));
					}
				}
			}
    		print(directory);
    		//msg.obj=directory;
			//handler.sendMessage(msg);
		}//while(true) file share
		//out.close();    
		//socket.close();
	}
}
